class Solution {
public:
    class compare
    {
    public:
        bool operator()(const pair<string, int> x1, const pair<string, int> x2)
        {
            return x1.second > x2.second || (x1.second == x2.second && x1.first < x2.first);

        }

    };
    vector<string> topKFrequent(vector<string>& words, int k)
    {
        map<string, int> m;
        for (auto& x : words)
        {
            m[x]++;
        }
        vector<pair<string, int>> vt(m.begin(), m.end());
        sort(vt.begin(), vt.end(), compare());
        vector<string> VT;
        for (int i = 0; i < k; i++)
        {
            VT.push_back(vt[i].first);

        }
        return VT;

    }
};